#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> v = {1, 2, 3, 4, 3};
    for (auto it = v.begin(); it != v.end();)
    {
#if 0 /*这样写会导致迭代器失效*/
        if (*it == 3)
        {
            v.erase(it);
        }
#else
        if (*it == 3)
        {
            it = v.erase(it); /*erase的返回值是下一个元素的迭代器*/
        }
        else
        {
            it++;
        }

#endif
    }
    for (int num : v)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}